<?php

/**
 * 文件上传 类库
 * @copyright Copyright&copy; 2015, 广州六象元一网络科技有限公司
 * @author 王海龙 $
 * @version  $Id: classUpload.php, v 1.0, 2015-01-01 王海龙 $
 */
class classUpload {
    /**
     * 图片上传的处理函数
     * @param   array     $upload    上传的文件
     * @param   string    $dir       上传目录
     * @param   string    $imgName   文件名称
     * @param   string    $type      文件类型
     * @param   string    $identify  上传文件的标识
     */
    function upload($upload, $dir = '', $name = '', $type = 'img', $identify = 'upload')
    {
        $this->size     = $upload['size'];
        $this->identify = $identify;
        $this->title    = $upload['name'];
        $this->type     = $upload['type'];
        /* 初始化目录 */
        if (empty($dir)) {
            $dir = date('Ym');
        }

        /* 检验文件类型 */
        if (!empty($type) && !$this->checkType($type, $upload['type'])) {
            $this->errorMsg = '上传文件类型错误';
            $this->errorNo  = 1101;
            return false;
        }

        /* 获取文件名 */
        if (empty($name)) {
            $name = md5(microtime()) . '.' . pathinfo($upload['name'], PATHINFO_EXTENSION);
        }

        /* 图片转移至缓存目录 */
        $tmpUrl = ROOT . 'temp/' . $name;
        if (move_uploaded_file($upload['tmp_name'], $tmpUrl)) {
            /* FTP上传 */
            $this->url = F()->url . F()->upload($tmpUrl, $dir . '/' . $name);

            /* 如果标识需要生成多个缩略图 */
            if (($identify == 'gallery' || $identify == 'goods') && !empty($GLOBALS['_CFG'][$identify]['thumb'])) {
                $this->thumb = array();
                foreach ($GLOBALS['_CFG'][$identify]['thumb'] as $key => $value) {
                    // 缩略图名称
                    $thumbName = $name . '_' . $value['width'] . '*' . $value['height'] . '.' . pathinfo($name, PATHINFO_EXTENSION);
                    // 缩略图缓存
                    $thumbTmp = ROOT . 'temp/' . $thumbName;
                    I()->thumb($tmpUrl, $thumbTmp, $value['width'], $value['height']);
                    // 上传缩略图
                    $this->thumb[$key] = F()->url . F()->upload($thumbTmp, $dir . '/' . $thumbName);
                    @unlink($thumbTmp);
                }
            }

            // 删除缓存
            @unlink($tmpUrl);
            if (empty($this->url)) {
                // 上传FTP失败
                $this->state = 'LOSER';
                $this->errorMsg = '上传FTP失败';
                $this->errorNo  = 1103;
                return false;
            } else {
                $this->state = 'SUCCESS';
                /* 将上传文件信息保存至数据库 */
                $img = array(
                    "url"      => $this->url,
                    "type"     => $this->type,
                    "name"     => $this->title,
                    "size"     => $this->size,
                    "identify" => $this->identify,
                    );
                D()->autoExecute(T('upload'), $img, 'INSERT');
                $this->id = D()->insert_id();
                return $this->url;
            }
        } else {
            // 转移失败
            $this->errorMsg = '本地转存失败';
            $this->errorNo  = 1104;
            return false;
        }

    }

    /**
     * 检查图片类型
     * @param   string  $type   图片类型
     * @return  bool
     */
    function checkType($type, $uploadType)
    {
        if ($type == 'img') {
            return $uploadType == 'image/pjpeg' ||
                   $uploadType == 'image/x-png' ||
                   $uploadType == 'image/png'   ||
                   $uploadType == 'image/gif'   ||
                   $uploadType == 'image/jpeg';
        }
    }

    /**
     * 获取文件信息
     */
    function getFileInfo()
    {
        return array(
            "id"       => $this->id,
            "state"    => $this->state,
            "url"      => $this->url,
            "type"     => $this->type,
            "title"    => $this->title,
            "size"     => $this->size,
            "identify" => $this->identify,
        );

    }
}